/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Screens;

import GamePlay.CanvasGame;
import Resources.InputKey;
import Resources.MenuResource;
import Resources.Resource;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author ThanhTrung
 */
public class Menu extends IScreen {

    public Menu(CanvasGame canvas) {
        super(canvas);
        init();
    }

    public void init() {
        imgs = new Image[MenuResource.img.length];
        load_screen();
    }

    public void load_screen() {
        imgs = Resource.load(MenuResource.img);
    }

    public void un_load_screen() {
        Resource.unload(imgs);
    }

    public void keyEvent(int keycode, int event) {
        if (event == InputKey.KEY_EVENT_DOWN) {
            switch (keycode) {
                case InputKey.KEY_UP:
                    MenuResource.CURR_MENU = (MenuResource.CURR_MENU - 1 + MenuResource.MAX_MENU) % MenuResource.MAX_MENU;
                    positionY();
                    break;
                case InputKey.KEY_DOWN:
                    MenuResource.CURR_MENU = (MenuResource.CURR_MENU + 1) % MenuResource.MAX_MENU;
                    positionY();
                    break;
                case InputKey.KEY_OK:
                    actionClick(MenuResource.CURR_MENU);
                    positionY();
                    break;
            }
        }
    }

    public void actionClick(int i) {
        switch (i) {
            case MenuResource.NEWGAME:
                Runtime.getRuntime().gc();
                un_load_screen();
                canvas.setScreen(new Game(canvas));
                break;
            case MenuResource.CONTINUE:
                Runtime.getRuntime().gc();
                un_load_screen();
                canvas.setScreen(new Game(canvas));
                break;
            case MenuResource.OPTIONS:
                Runtime.getRuntime().gc();
                un_load_screen();
                canvas.setScreen(new Options(canvas));
                break;
            case MenuResource.HIGHSCORE:
                Runtime.getRuntime().gc();
                un_load_screen();
                canvas.setScreen(new HighScore(canvas));
                break;
            case MenuResource.GUIDE:
                Runtime.getRuntime().gc();
                un_load_screen();
                canvas.setScreen(new Guide(canvas));
                break;
            case MenuResource.ABOUT:
                Runtime.getRuntime().gc();
                un_load_screen();
                canvas.setScreen(new About(canvas));
                break;
            case MenuResource.EXIT:
                Runtime.getRuntime().gc();
                un_load_screen();
                canvas.setScreen(new Exit(canvas));
                break;
        }
    }

    public void positionY() {
        MenuResource.CURR_MENU_Y = 40 + 25 * MenuResource.CURR_MENU;
    }

    public void update() {
    }

    public void paint(Graphics g) {
        g.setColor(0, 0, 0);
        g.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
        g.drawImage(imgs[MenuResource.IMG_SPLASH], 0, 0, Graphics.TOP | Graphics.LEFT);
        g.drawImage(imgs[MenuResource.IMG_BUTTON], 130, MenuResource.CURR_MENU_Y, Graphics.TOP | Graphics.LEFT);

        g.drawImage(imgs[MenuResource.IMG_MENU], MENU_X,MENU_Y, Graphics.TOP | Graphics.LEFT);
        
    }
    private Image[] imgs;
    private static final int MENU_X = 150;
    private static final int MENU_Y = 35;
}
